package com.buxiaoxia.system.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.*;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 退出登录删除redis存储的token信息  防止refresh_token
 * Created by 罗昆 on 2018/9/21.
 */
@Configuration
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {


    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        String token =httpServletRequest.getHeader("Authorization");
        String accessToken=token.split(" ")[1];
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "bearer " + accessToken);
        ResponseEntity<String> response = new TestRestTemplate().exchange("http://127.0.0.1:8081/logout", HttpMethod.GET,
                new HttpEntity<>(null, headers), String.class);
        httpServletResponse.setStatus(response.getStatusCode().value());
        System.out.print(response.getStatusCode().value());
    }


}
